<template>
  <el-form>
    <el-form-item label="Name">
      <el-input v-model.trim="user.info.admin" :disabled="true" />
    </el-form-item>
    <el-form-item label="Password">
      <el-input v-model.trim="password" clearable type="password" placeholder="请输入你要修改的密码" />
    </el-form-item>
    <el-form-item label="Phone">
      <el-input v-model="user.info.phone" clearable maxlength="11" />
    </el-form-item>
    <el-form-item label="Email">
      <el-input v-model.trim="user.info.email" clearable />
    </el-form-item>
    <el-form-item label="Introduction">
      <el-input v-model.trim="user.info.introduction" clearable />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">Update</el-button>
    </el-form-item>
  </el-form>
</template>

<script>

export default {
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          name: '',
          email: '',
          info: {

          }
        }
      }
    }
  },
  data() {
    return {
      password: "",
    }
  },
  methods: {
    checkTelephone(telephone) {
      var reg = /^[1][3,4,5,7,8][0-9]{9}$/;
      if (!reg.test(telephone)) {
        return false;
      } else {
        return true;
      }
    },
    submit() {
      // this.$message({
      //   message: 'User information has been updated successfully',
      //   type: 'success',
      //   duration: 5 * 1000
      // })
      let args = { password: this.password, ...this.user.info }
      const { password, admin, phone } = args
      if (password == "") this.$message.error(' 密码不能为空 ')
      else if (password.length <= 4) this.$message.error(' 密码长度需大于4位 ')
      else if (phone == "") this.$message.error(' 手机号不能为空 ')
      // else if (this.checkTelephone(phone)) this.$message.error( '手机号格式有误' )
      else {
        this.$prompt('为了系统安全，请验证登录密码', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({ value }) => {
          /**
           * 校验密码
           */
          this.$store.dispatch('user/validPassword', { password: value, admin })
            .then((response) => {
              if (response) {
                this.$message.success("密码校验成功");
                /**
                 * 修改操作
                 */
                this.$store.dispatch('user/edit', args)
                  .then((response) => {
                    this.$message.success(" 管理员信息修改成功，请重新登录 ")
                    setTimeout(() => {
                      this.$store.dispatch('user/logout')
                      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
                      sessionStorage.removeItem('userInfo')
                      this.$router.go(0)
                    }, 2000);
                  })
                  .catch((error) => {
                    this.$message.error(error)
                  })
              } else {
                this.$message.error("密码校验失败,请重新输入");
                this.password = ""
              }
            })
            .catch((error) => {
              this.$message.error(error);
            })
        }).catch(() => {
          this.$message({
            type: 'warning',
            message: '取消输入'
          });
        });
      }
    }
  }
}
</script>
